package com.shwread.android.ui.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.shwread.android.qysw10000204.R;

import logic.util.PicassoUtil;
import logic.util.Util;


/**
 * 书籍封面自定义View
 *
 * @author 刘宾
 * @date 2015年3月26日 上午10:20:13
 */
public class BookCoverView
		extends
		RelativeLayout
{
	private Context context;
	private ImageView ivCover, ivTag;

	public BookCoverView(Context context) {
		super(context);
		initView(context);
		this.context = context;
	}

	public BookCoverView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
		this.context = context;
	}

	@SuppressLint ("NewApi")
	public BookCoverView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView(context);
		this.context = context;
	}

	private void initView(Context context) {
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		RelativeLayout containerView = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.bookcover_view, null);

		ivCover = (ImageView) containerView.findViewById(R.id.bookcover_view_cover_img);
		ivTag = (ImageView) containerView.findViewById(R.id.bookcover_view_tag_img);

		addView(containerView, lp);
	}

	/**
	 * 设置默认的书籍封面
	 *
	 * @param resId
	 */
	public void setDefaultBookCover(int resId) {
		if (null != ivCover) ivCover.setImageResource(resId);
	}

	/**
	 * 设置书籍封面、标签的url
	 *
	 * @param coverUrl
	 *            封面url
	 * @param tagUrl
	 *            标签url
	 */
	public void setImageUrl(String coverUrl, String tagUrl) {
		if (Util.isNotEmpty(coverUrl) && null != ivCover) {
//			ivCover.setImageUrl(coverUrl);
			PicassoUtil.load(context,coverUrl, R.drawable.bookcover_default,ivCover);
		}
		if (Util.isNotEmpty(tagUrl) && null != ivTag)
		{
			ivTag.setVisibility(View.VISIBLE);
//			ivTag.setImageUrl(tagUrl);
			PicassoUtil.load(context,tagUrl, ivTag);
		}
		else ivTag.setVisibility(View.GONE);
	}

}
